
const app = getApp()
import {
  sendUserLogin
} from '../../../network/other'
import {
  toast,
  throttle
} from '../../../utils/Tools'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: null,
    tabIndex: 0,
    sms: null,
    text: '获取验证码',
    isSend: false,
    isDisabled: false
  },
  onReady: function () {
    this.dialog = this.selectComponent("#dialog")
  },
  showDialog() {
    this.dialog.showDialog();
  },
  //取消事件
  _cancelEvent() {

    this.dialog.hideDialog();
  },
  //确认事件
  _confirmEvent() {

    this.dialog.hideDialog();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // throttle(this.formSubmit,2000)
    // this.formSubmit = throttle(this.formSubmit, 2000)
  },
  async formSubmit(e) { //  帐号登录点击验证
    console.log("点击了登录按钮");
    const {
      username,
      password
    } = e.detail.value
    const data = {
      account: username,
      pwd: password
    }
    if (username === '' || password === '') {
      toast('请填写完整的信息', 'none')
    } else {
      const res = await sendUserLogin(data);
      if (res.code === 200) {
        wx.setStorageSync('token', res.data.jwt_token)
        let newUser = {
          user: username,
          password: password
        }
        // 判断是否有成功登陆账号
        if (!wx.getStorageSync('account')) {
          
          wx.setStorageSync('account', [newUser])
        } else {
          let getUser = wx.getStorageSync('account')

         
          getUser.push(newUser)

          wx.setStorageSync('account', getUser)
          // 对象去重
          const value = wx.getStorageSync('account')
          var result = [];
          var obj = {};
          for (var i = 0; i < value.length; i++) {
            if (!obj[value[i].user]) {
              result.push(value[i]);
              obj[value[i].user] = true;
            }
          }
          wx.setStorageSync('account',result)
        }
        toast('登录成功', 'success')
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/order/order/order'
          })
        }, 1500)
      } else {
        toast(res.message, 'none')
      }
    }
  },
})